package tgeo.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ExtendedModifyEvent;
import org.eclipse.swt.custom.ExtendedModifyListener;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.ViewPart;

public class StyledTextView extends ViewPart {
	private static final String PUNCTUATION = "(){};!&|.+-*/";
	Color red = null;
	  
	StyledText styledText = null;
	public StyledTextView() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		Display display = parent.getDisplay();
		red = display.getSystemColor(SWT.COLOR_RED);

	    
		styledText = new StyledText(parent, SWT.BORDER|SWT.WRAP);
//		styledText.addVerifyListener(new VerifyListener() {
//			
//			@Override
//			public void verifyText(VerifyEvent e) {
//				e.text = e.text.replaceAll("[^0-9a-fA-F]", "").toUpperCase();
//			}
//		});
		styledText.addExtendedModifyListener(new ExtendedModifyListener() {
			@Override
			public void modifyText(ExtendedModifyEvent event) {
				int end = event.start + event.length - 1;
		        if (event.start <= end) {
		            // Get the text
					String text = styledText.getText(event.start, end);

					// Create a collection to hold the StyleRanges
					java.util.List ranges = new java.util.ArrayList();
					
					// Turn any punctuation red
					for (int i = 0, n = text.length(); i < n; i++) {
						if( ((event.start+i)/2)%2 == 0){
							ranges.add(new StyleRange(event.start + i, 1, red,
							null, SWT.BOLD));
						}
//						if (PUNCTUATION.indexOf(text.charAt(i)) > -1) {
//							ranges.add(new StyleRange(event.start + i, 1, red,
//									null, SWT.BOLD));
//						}
					}

					// If we have any ranges to set, set them
					if (!ranges.isEmpty()) {
						styledText.replaceStyleRanges(event.start,
								event.length, (StyleRange[]) ranges
										.toArray(new StyleRange[0]));
					}
				}
				
			}
		});
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
